package com.example.demo;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: zm
 * Date: 2025/2/4
 */

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String hello(){
        return "hello,SpringBoot";
    }
    @RequestMapping("/helloCN")
    public String helloCN(){
        return "hello bite";
    }

    @RequestMapping("/r1")
    public String r1(String name, Integer age){
        return "接受到的信息是: name:" + name + "  age:" + age;
    }

    //传递对象
    @RequestMapping("/r2")
    public String r2(Userinfo user){
        return user.toString();
    }

    //参数重命名
    @RequestMapping("/r3")
    public String r3(@RequestParam(value="name", required=false) String username){
        return "重命名后: " + username;
    }
    //传递数组
    @RequestMapping("/r4")
    public String r4(String[] arr){
        return Arrays.toString(arr);
    }
    //传递集合
    @RequestMapping("/r5")
    public String r5(@RequestParam("list") List<String> list){
        return list.toString() + ",size: "+ list.size();
    }
    //传递json数据
    @RequestMapping("/r8")
    public String r8(@RequestBody Userinfo userinfo){
        return userinfo.toString();
    }
    //获取URL中的参数
    @RequestMapping("/r6/{articleId}")
    public String r6(@PathVariable Integer articleId){
        return "articleId:"+articleId;
    }
    //修改参数
    @RequestMapping("/r7/{name}/{age}")
    public String r7(@PathVariable("name") String username, @PathVariable Integer age){
        return "username: " + username + " age: " + age;
    }

    //上传文件
    @RequestMapping("/r9")
    public String r9(MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename();
        file.transferTo(new File("C:\\Users\\14225\\Pictures\\Saved Pictures\\123.jpg"+fileName));
        return "上传的文件: "+file.getOriginalFilename();
    }


}
